package cn.stylefeng.guns.sys.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxd;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxda;
import cn.stylefeng.guns.sys.modular.system.entity.BtyPsda;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdService;
import cn.stylefeng.guns.sys.modular.system.service.BtyPsdaService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 对账单控制器
 * @author liuy
 * @Date
 */
@Controller
@RequestMapping("/btyPsda")
public class BtyPsdaController extends BaseController {

    private String PREFIX = "/modular/system/btyPsda/";
    @Resource
    private BtyBxdService btyBxdService;
    @Resource
    private BtyPsdaService btyPsdaService;
    /**
     * 跳转到通知列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyPsda.html";
    }
    /**
     * 获取对账单列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,String status,String flag,String timeLimit,String username,String dm) {
        //拼接查询条件
        String beginTime = "";
        String endTime = "";
        if (ToolUtil.isNotEmpty(timeLimit)) {
            String[] split = timeLimit.split(" - ");
            beginTime = split[0];
            endTime = split[1];
            endTime=split[1]+" 23:59:59";
        }
        //获取用户的帐号
        String account= LoginContextHolder.getContext().getUser().getName();
        Long userId=LoginContextHolder.getContext().getUserId();
        List<String> roleNames= LoginContextHolder.getContext().getUser().getRoleNames();
        Page<Map<String, Object>> list;
        if(account.equals("admin")|roleNames.contains("出纳")){
            status="1";
            list = this.btyPsdaService.list(condition,status,flag,beginTime,endTime,username,dm);
        }else {
            //status="0";
            list = this.btyPsdaService.list(condition,status,flag,beginTime,endTime,account,dm);;
        }
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }
    /**
     * 跳转到添加档案
     */
    @RequestMapping("/btyPsda_add")
    public String noticeAdd() {
        return PREFIX + "btyPsda_add.html";
    }
    /**
     * 跳转到修改档案
     */
    @RequestMapping("/btyDzd_update/{id}")
    public String noticeUpdate(@PathVariable Long id, Model model) {
        BtyPsda btyDzd = this.btyPsdaService.getById(id);
        model.addAllAttributes(BeanUtil.beanToMap(btyDzd));
        LogObjectHolder.me().set(btyDzd);
        return PREFIX + "btyPsda_edit.html";
    }
    /**
     * 修改档案
     * @author liuy
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BtyPsda btyDzd) {
        if (ToolUtil.isOneEmpty(btyDzd,btyDzd.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.btyPsdaService.updateById(btyDzd);
        return SUCCESS_TIP;
    }

    /**
     * 新增报销单
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestParam String dm,@RequestParam String px,@RequestParam String xm,
                      @RequestParam String yhk,@RequestParam String ye,@RequestParam String tel){
        BtyPsda btyDzd=new BtyPsda();
        btyDzd.setDm(dm);
        btyDzd.setXm(xm);
        btyDzd.setPx(px);
        btyDzd.setYhk(yhk);
        btyDzd.setYe(ye);
        btyDzd.setTel(tel);
        btyDzd.setCreateUser(LoginContextHolder.getContext().getUserId());
        btyDzd.setCreateTime(new Date());
        this.btyPsdaService.save(btyDzd);
        return SUCCESS_TIP;
    }

    /**
     * 档案详情
     */
    @RequestMapping("/info")
    @ResponseBody
    public Object getThemeInfo(@RequestParam String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new RequestEmptyException();
        }
        String account= LoginContextHolder.getContext().getUser().getName();
        List<String>roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        BtyPsda btyDzd=btyPsdaService.getById(id);
//        if(account.equals("admin")|roleNames.contains("超级管理员")|roleNames.contains("出纳")){
//            btyBxd.setRoleFlag(0);
//        }else{
//            btyBxd.setRoleFlag(1);
//        }
        return btyDzd;
    }
    /**
     * 删除对账单
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long id) {
        //BtyBxd btybxd=btyBxdService.getById(id);
//        //获取已报销状态
//        Integer flag=btybxd.getFlag();
//        if(flag==0){
//            this.btyBxdService.removeById(id);
//        }else{
//            return 2;
//        }
          this.btyPsdaService.removeById(id);
        return SUCCESS_TIP;
    }
    /**
     * 查询配送人
     * (@RequestParam(value = "dm",required = false) String dm
     */
    @RequestMapping(value = "/getPsr")
    @ResponseBody
    public List<BtyBxda> getBxr(){
        //获取是否是督导
        String rem=LoginContextHolder.getContext().getUser().getAvatar();
        //获取用户的帐号
        String account= LoginContextHolder.getContext().getUser().getName();
        String phone=LoginContextHolder.getContext().getUser().getAccount();
       List<String> roleNames= LoginContextHolder.getContext().getUser().getRoleNames();
        List<BtyBxda> list=btyPsdaService.psrList(account);
        return list;
    }
    /**
     * 查询配送人查询对应的店名
     */
    @RequestMapping(value = "/getDm")
    @ResponseBody
    public List<BtyBxd> getDm(String xm){
        String account= LoginContextHolder.getContext().getUser().getName();
        List<BtyBxd> list;
        if(ToolUtil.isEmpty(xm)){
            list=btyPsdaService.dmList(account);
        }else{
            list=btyPsdaService.dmList(xm);
        }
        return list;
    }
    /**
     * 查询店名对应的品项
     */
    @RequestMapping(value = "/getPx")
    @ResponseBody
    public List<BtyPsda> getPx(String dm){
        String account= LoginContextHolder.getContext().getUser().getName();
        List<BtyPsda> list=btyPsdaService.pxList(dm,account);
        return list;
    }
    /**
     * 查询会计
     */
    @RequestMapping(value = "/getKj")
    @ResponseBody
    public List<BtyBxda> getKj(){
        //获取是否是督导
        String rem=LoginContextHolder.getContext().getUser().getAvatar();
        //获取用户的帐号
        String account= LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames= LoginContextHolder.getContext().getUser().getRoleNames();
        List<BtyBxda> list;
        if(account.equals("admin")|roleNames.contains("出纳")){
            list = this.btyBxdService.kjList(null);
        }else {
            list =this.btyBxdService.kjList(account);
        }
        return list;
    }
    /**
     * 批量审核确认
     * @author liuy
     * @Date
     */
    @RequestMapping(value = "/confirmOk")
    @ResponseBody
    public Object confirmOk(@RequestParam String ids) {
        String[] idss = ids.split(",");
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        for(String id:idss){
            BtyBxd btyBxd=btyBxdService.getById(id);
            if(roleNames.contains("会计")){
                Integer status=btyBxd.getStatus();
                if(status>0){
                  return 1;
                }
                btyBxd.setStatus(1);
            }else if(roleNames.contains("出纳")){
                Integer status=btyBxd.getStatus();
                if(status>1){
                    return 2;
                }
                btyBxd.setStatus(9);
            }
            btyBxdService.updateById(btyBxd);
        }
        return SUCCESS_TIP;
    }
}
